(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;	
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
		});
	});	
	function leave(){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var flag = "0";
		var createtime = strToDate(getFormatDate(new Date(),"yyyy-MM-dd"))/1000;
		var reason = encodeURI($("#askforleave-reason").val());
		var starttime = strToDate($("#askforleave-starttime").val())/1000;
		var endtime = strToDate($("#askforleave-endtime").val())/1000;
		var type = $("#askforleave-type option:selected").val();
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var obj = {
				flag:flag,
				studentcode:studentcode,
				createtime:createtime,
				reason:reason,
				starttime:starttime,
				endtime:endtime,
				type:type,
		};
		var data = {
				cmd:"add",
				model:"leave",
				userKey:userKey,
				data:obj,
				remark:"",					
		};
		processBegin("正在应用中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1){
				processEnd("请假成功");
			}else{
				processError("请假失败");
			}
		},null,null);
	}
	function checkParames(){
		if($("#askforleave-starttime").val() == ""){
			$("#askforleave-starttime").focus();
			$("#askforleave-starttime-ex").html("不能为空");
			return false; 
		}else if($("#askforleave-endtime").val() == ""){
			$("#askforleave-endtime").focus();
			$("#askforleave-endtime-ex").html("不能为空");
			return false; 
		}else if($("#askforleave-reason").val() == ""){
			$("#askforleave-reason").focus();
			$("#askforleave-reason-ex").html("不能为空");
			return false; 
		}else{
			return true;
		}
	}
	
	function check2(){
		var flg = false;
		var checkFails = $(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	
	function pagebtns(){
		var name = $(".students-choice-select",parent.document).attr("name");
		$("#askforleave-name").val(name);
		$("#askforleave-name").attr("readonly","readonly");
		$("#askforleave-name").css("background-color","rgb(229, 229, 229)");
		var time = getFormatDate(new Date(),"yyyy-MM-dd");
		$("#askforleave-starttime").val(time + " 08:00");
		$("#askforleave-endtime").val(time + " 23:59");	
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("students-choice-select");
			var name = $(".students-choice-select",parent.document).attr("name");
			$("#askforleave-name").val(name);
			var time = getFormatDate(new Date(),"yyyy-MM-dd");
			$("#askforleave-starttime").val(time + " 08:00");
			$("#askforleave-endtime").val(time + " 23:00");			
		});	
		$("#askforleave").unbind("click");
		$("#askforleave").click(function(){ 
			if(checkParames()){
				if(check2()){
					leave();
				}
			}
		});
		check("askforleave-starttime",isNull,"null");
		check("askforleave-endtime",isNull,"null");
		check("askforleave-reason",isNull,"null");
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();